package com.beck.code.generator.plugin;

import cn.hutool.core.io.resource.NoResourceException;
import cn.hutool.core.util.StrUtil;
import com.beck.code.generator.configProperty.Configuration;
import com.beck.code.generator.generatorProcessor.ContextGenerator;


import javax.xml.parsers.ParserConfigurationException;

/**
 * @author CuiJianbo
 * @since 2025/7/1
 */

public class JavaCodeGenerator {

    public static void generateCode() {
        Configuration config = null;
        try {
            config = ConfigurationParser.getInstance().parseXmlConfigFile();
        } catch (NoResourceException e) {
            throw new RuntimeException("resources folder don't have generatorConfig.xml");
        } catch (ParserConfigurationException e) {
            throw new RuntimeException("parse generatorConfig.xml error");
        } catch (Exception e) {
            String errorMsg = (StrUtil.isNotEmpty(e.getMessage()) ? e.getMessage() : "");
            throw new RuntimeException(errorMsg);
        }


        //构造上下文环境
        ContextGenerator contextGenerator = ContextGenerator.getContextGenerator(config.getContext());

        //获取表字段数据
        contextGenerator.generateIntrospectedTables();

        //生成java代码
        contextGenerator.generateCode();


    }


}
